<template>
    <div class="confirm-wrap">
        <el-header class="exam-header">
            <div class="header-content">
                <div></div>
                <!-- 系统标题 -->
                <div class="system-title">
                    <el-icon class="title-icon">
                        <Reading />
                    </el-icon>
                    <span class="text-green-600 font-bold text-xl">食品安全在线考试系统</span>
                </div>

                <!-- 用户信息区 -->
                <div class="user-panel" v-if="userInfo">
                    <!-- 头像 -->
                    <el-avatar :size="40"
                        :src="userInfo.avatar ? userInfo.avatar : 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'"
                        class="mr-2" />
                    <!-- 考生信息 -->
                    <el-dropdown>
                        <span class="user-info">
                            <span class="username">{{ userInfo.userName }}</span>
                            <span class="nickname">{{ userInfo.nickName }}</span>
                        </span>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item @click="handleLogout">
                                    <el-icon>
                                        <SwitchButton />
                                    </el-icon>退出登录
                                </el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                </div>
            </div>
        </el-header>
        <div class="confirm-exam-container">
            <!-- 标题区域 -->
            <div class="header-section">
                <div class="title-box">
                    <el-icon class="title-icon">
                        <DocumentChecked />
                    </el-icon>
                    <h1 class="title-text">考试信息确认</h1>
                </div>
                <div class="system-status">
                    <span class="time-info">
                        <el-icon>
                            <Clock />
                        </el-icon>
                        系统时间：{{ new Date().toLocaleString() }}
                    </span>
                </div>
            </div>

            <!-- 主内容区两栏布局 -->
            <div class="main-content">
                <!-- 考生信息模块 -->
                <el-card class="info-panel glow-card" v-if="userInfo">
                    <div class="panel-header">
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span class="panel-title">考生信息核验</span>
                    </div>
                    <el-descriptions :column="1" class="info-detail">
                        <el-descriptions-item label="考生账号">
                            <el-tag type="info">{{ userInfo.userName }}</el-tag>
                        </el-descriptions-item>
                        <el-descriptions-item label="考生姓名">
                            <span class="highlight-text">{{ userInfo.nickName }}</span>
                        </el-descriptions-item>
                        <el-descriptions-item label="考生性别">
                            <span class="gender-tag">{{ userInfo.sex === '1' ? '女' : '男' }}</span>
                        </el-descriptions-item>
                        <el-descriptions-item label="身份证号">
                            <span class="highlight-text">{{ userInfo.idCardNo }}</span>
                        </el-descriptions-item>
                        <el-descriptions-item label="所属机构">
                            <span class="highlight-text">{{ depart }}</span>
                        </el-descriptions-item>
                    </el-descriptions>
                </el-card>

                <!-- 右侧选项卡区域 -->
                <div class="right-panel">
                    <el-card class="glow-card">
                        <el-tabs v-model="activeTab">
                            <el-tab-pane label="待考试场次" name="pending">
                                <div class="exam-detail" v-if="examDetail">
                                    <h3 class="exam-title">{{ examDetail.name }}</h3>
                                    <div class="exam-info-grid">
                                        <div class="info-item">
                                            <el-icon>
                                                <Timer />
                                            </el-icon>
                                            <span>考试时长：{{ examDetail.examDurationMinutes }}分钟</span>
                                        </div>
                                        <div class="info-item">
                                            <el-icon>
                                                <Medal />
                                            </el-icon>
                                            <span>满分：{{ examDetail.allMark }}分</span>
                                        </div>
                                        <div class="info-item">
                                            <el-icon>
                                                <Flag />
                                            </el-icon>
                                            <span>及格分数：{{ examDetail.passMark }}分</span>
                                        </div>
                                    </div>
                                    <el-button type="success" class="start-btn" @click="handleExamAction">
                                        <el-icon>
                                            <Promotion />
                                        </el-icon>
                                        开始考试
                                    </el-button>
                                </div>
                                <el-empty v-else description="暂无待考场次" />
                            </el-tab-pane>

                            <el-tab-pane label="已考试列表" name="finished">
                                <el-table max-height="400" v-if="examListed" :data="examListed" class="history-table">
                                    <el-table-column prop="name" label="考试名称">
                                        <template #header>
                                            考试名称
                                        </template>
                                    </el-table-column>
                                    <el-table-column prop="examMark" label="分数" />
                                    <el-table-column label="状态">
                                        <template #default="{ row }">
                                            <el-tag :type="getStatusType(row.result)" effect="dark">
                                                {{ row.resultText }}
                                            </el-tag>
                                        </template>
                                    </el-table-column>

                                    <!-- <el-table-column label="操作">
                                        <template #default="{ row }">
                                            <el-button @click="handleExamAction1(row.id)"
                                                :type="row.status === '0' ? 'primary' : 'info'"
                                                :disabled="row.status !== '0'" class="action-btn">
                                                开始考试
                                            </el-button>
                                        </template>
                                    </el-table-column> -->
                                </el-table>
                                <el-empty v-else description="暂无考试数据" />
                            </el-tab-pane>
                        </el-tabs>
                    </el-card>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getFromLocal } from '@/utils/index.js'
import { examApi } from '@/api/exam'
import emitter from '../utils/emit'
import { ElMessageBox } from 'element-plus'
import { SwitchButton, Reading, Clock, DocumentChecked, UserFilled, Tickets, List, AlarmClock, Timer, Medal, Flag, Promotion } from '@element-plus/icons-vue'
const userInfo = ref()
const router = useRouter()
const depart = ref()

// 新增选项卡切换逻辑
const activeTab = ref('pending')

console.log('54455445');
// 模拟考试数据
const examList = ref([
    { id: 1, title: '食品安全基础考试', status: 'pending' },
    { id: 2, title: '卫生管理考试', status: 'finished', score: 85 },
    { id: 3, title: '应急预案考试', status: 'timeout' },
    { id: 3, title: '应急预案考试', status: 'timeout' },
    { id: 3, title: '应急预案考试', status: 'timeout' },
    { id: 3, title: '应急预案考试', status: 'timeout' },
    { id: 3, title: '应急预案考试', status: 'timeout' },
])

const getStatusType = (status) => {
    const types = {
        0: 'danger',
        1: 'success',
    }
    return types[status]
}


const handleLogout = () => {
    // 退出登录逻辑
    ElMessageBox.confirm('确定要退出登录吗？', '退出提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
    }).then(() => {
        // 确认退出逻辑
        localStorage.removeItem('exam_token')
        localStorage.removeItem('userinfo')
        localStorage.removeItem('login_info')
        router.push('/login')
    }).catch(() => {
        // 取消操作
        console.log('取消退出')
    })
}

// 状态映射优化
const statusTextMap = {
    pending: '待考试',
    finished: '已通过',
    timeout: '已超期'
}
//获取登录信息
const getUserinfo = async () => {
    examApi.getProfile().then(res => {
        console.log(res);
        if (res.code == 200) {
            userInfo.value = res.data.user
            depart.value = res.data.departName
            localStorage.setItem('userinfo', JSON.stringify(res.data.user))
        }
    })
}
getUserinfo()
const examListed = ref()
const examDetail = ref()
//获取考试列表
const getExam = () => {
    examApi.getExamList({}).then(res => {
        examDetail.value = res.data
    })
    examApi.getExamListed({}).then(res => {
        examListed.value = res.data
    })
}
getExam()
//开始考试
const handleExamAction = (e) => {
    console.log(examDetail.value.paperId, 55);
    router.push({
        name: 'exam',
        params: {
            paperId: examDetail.value.paperId  // 确保是对象形式
        }
    })
}
const handleExamAction1 = (id) => {
    console.log(id);

    router.push({
        name: 'exam',
        params: {
            paperId: id  // 确保是对象形式
        }
    })
}
</script>
<style lang="scss" scoped>
.confirm-wrap {
    height: 100%;
}

.el-container {
    height: 100%;
}

.el-main {
    padding: 15px;
    height: 100%;
}

/* 头部容器 */
.exam-header {
    background-color: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    border-bottom: 1px solid #e4e7ed;
    height: 64px;
}

/* 头部内容容器 */
.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    // max-width: 1200px;
    height: 100%;
    margin: 0 auto;
    padding: 0 5%;
}

/* 系统标题 */
.system-title {
    display: flex;
    align-items: center;
}

.title-icon {
    color: #13ce66;
    font-size: 28px;
    margin-right: 8px;
}

.system-title span {
    color: #13ce66;
    font-weight: 700;
    font-size: 20px;
}

/* 用户信息面板 */
.user-panel {
    display: flex;
    align-items: center;
    gap: 12px;
}

/* 用户信息文字 */
.user-info {
    display: flex;
    flex-direction: column;
    cursor: pointer;
}

.username {
    color: #666;
    font-size: 12px;
    line-height: 1.4;
}

.nickname {
    color: #333;
    font-weight: 500;
    font-size: 14px;
    line-height: 1.4;
}

/* 下拉菜单图标 */
.el-dropdown-menu__item .el-icon {
    margin-right: 8px;
}

/* 头像 */
.el-avatar {
    border: 2px solid #e8e8e8;
    transition: border-color 0.3s;
}

.el-avatar:hover {
    border-color: #13ce66;
}

.confirm-exam-container {
    background: #f8fafb;
    height: calc(100% - 128px);
    border-radius: 8px;

    .header-section {
        margin-bottom: 32px;

        .title-box {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;

            .title-icon {
                color: #13ce66;
                font-size: 42px;
            }

            .title-text {
                color: #13ce66;
                font-size: 38px;
                margin: 0;
            }
        }

        .system-status {
            text-align: center;

            .time-info {
                display: inline-flex;
                align-items: center;
                gap: 8px;
                color: #606266;
                font-size: 14px;
            }
        }
    }

    .main-content {
        max-width: 1400px;
        margin: 0 auto;
        margin-top: 80px;
        display: grid;
        grid-template-columns: 1fr 1.5fr;
        gap: 24px;
    }

    .glow-card {
        border: none;
        border-radius: 12px;
        box-shadow: 0 4px 16px rgba(19, 206, 102, 0.1);
        transition: transform 0.3s;


    }

    .panel-header {
        display: flex;
        align-items: center;
        gap: 8px;
        margin-bottom: 20px;
        padding-bottom: 16px;
        border-bottom: 1px solid #e8f4e9;

        .panel-title {
            font-size: 18px;
            color: #13ce66;
        }

        .el-icon {
            font-size: 24px;
            color: #13ce66;
        }
    }

    .info-detail {
        :deep(.el-descriptions__label) {
            width: 80px;
            color: #606266;
            font-weight: 500;
        }

        .highlight-text {
            color: #303133;
            font-weight: 500;
        }

        .gender-tag {
            padding: 4px 12px;
            border-radius: 4px;
            background: #e8f4e9;
            color: #13ce66;
        }

        .id-number {
            letter-spacing: 1px;
            font-family: monospace;
        }
    }

    .right-panel {
        .el-tabs {
            :deep(.el-tabs__header) {
                margin: 0 0 20px;

                .el-tabs__item {
                    font-size: 16px;
                    color: #606266;

                    &.is-active {
                        color: #13ce66;
                        font-weight: 500;
                    }
                }

                .el-tabs__active-bar {
                    background-color: #13ce66;
                }
            }

            .el-tab-pane {
                display: flex;
                justify-content: center;
            }
        }

        .exam-detail {
            width: 60%;
            padding: 16px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;

            .exam-title {
                color: #303133;
                margin-bottom: 24px;
                font-size: 20px;
            }

            .exam-info-grid {
                display: grid;
                gap: 16px;
                margin-bottom: 32px;

                .info-item {
                    display: flex;
                    align-items: center;
                    gap: 8px;
                    color: #606266;

                    .el-icon {
                        color: #13ce66;
                        font-size: 20px;
                    }

                    span {
                        font-size: 15px;
                    }
                }
            }

            .start-btn {
                width: 100%;
                padding: 12px;
                font-size: 16px;
                border-radius: 8px;
                transition: all 0.3s;

                &:hover {
                    transform: translateY(-2px);
                    box-shadow: 0 4px 12px rgba(19, 206, 102, 0.15);
                }
            }
        }

        .history-table {
            margin-top: -10px;

            :deep(.el-table__row) {
                transition: all 0.3s;

                &:hover {
                    box-shadow: 0 2px 8px rgba(19, 206, 102, 0.1);
                }
            }

            .action-btn {
                padding: 10px 16px;
                border-radius: 6px;
            }
        }
    }
}
</style>